package com.cakebackoffice;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.cakebackoffice.Model.CatalogCategory;
import com.cakebackoffice.Model.MenuType;
import com.cakebackoffice.Model.ProductType;

import java.util.List;


public class ListViewCatalogAdapter extends ArrayAdapter<Object> {
    int resource;

    public ListViewCatalogAdapter(Context context, int textViewResourceId, List<Object> objects) {
        super(context, textViewResourceId, objects);
        this.resource = textViewResourceId;
    }

    // On override ArrayAdapter
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LinearLayout objectView;
        // Récuperer l'order courant
        Object object = getItem(position);


        // Inflate the catalog_detail_product
        if (convertView == null) {
            objectView = new LinearLayout(getContext());
            String inflater = Context.LAYOUT_INFLATER_SERVICE;
            LayoutInflater vi = (LayoutInflater) getContext().getSystemService(inflater);
            vi.inflate(resource, objectView, true);
        } else {
            objectView = (LinearLayout) convertView;
        }

        // Récupérer les View des orderView
        TextView listViewText = (TextView) objectView.findViewById(R.id.ListCatalogText);
        ImageView icon = (ImageView) objectView.findViewById(R.id.CatalogListViewImage);


        // Remplissage des CatalogCategory
        if (object instanceof CatalogCategory) {
            listViewText.setText(((CatalogCategory) object).getName() + "/");
            Resources res = getContext().getResources();
            Drawable shape = res.getDrawable(R.drawable.ic_category);
            icon.setImageDrawable(shape);
        } else if (object instanceof ProductType) {
            listViewText.setText(((ProductType) object).getName());
            Resources res = getContext().getResources();
            Drawable shape = res.getDrawable(R.drawable.ic_product);
            icon.setImageDrawable(shape);
        } else if (object instanceof MenuType) {
            listViewText.setText(((MenuType) object).getName());
            Resources res = getContext().getResources();
            Drawable shape = res.getDrawable(R.drawable.ic_menu);
            icon.setImageDrawable(shape);
        } else {
            listViewText.setText((String) object);
        }

        return (objectView);


    }


}
